/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.controls
{
	import com.competentum.builders.BindsBuilder;
	import com.competentum.io.IEngineView;
	import com.competentum.renderers.DraggableItemRenderer;
	import com.competentum.vos.AbstractItemVO;
	import com.competentum.windows.Scope;
	import com.competentum.lib.data.builders.AbstractDataBuilder;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.collections.Grouping;
	import mx.collections.GroupingCollection2;
	import mx.collections.GroupingField;
	import mx.collections.HierarchicalCollectionView;
	import mx.collections.HierarchicalData;
	import mx.collections.ICollectionView;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridRendererProvider;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.ClassFactory;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.core.mx_internal;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	use namespace mx_internal;
	
	/**
	 * AdvancedDataGrid that has special abilities.
	 * 1. Configures its columns
	 * 2. Has auto scrolling while dragging item over
	 * 3. Invalidates renderers when needed
	 * 4. Can be refreshed bo refresh() method
	 * 
	 * @author Valentine Abanosimov, Sergey Osipov
	 * */
	public class ADG extends AdvancedDataGrid implements IEngineView
	{
		private const COLUMN_WIDTH:int = 125;
		private const DISPLAY_LABEL_STRING:String = 'displayLabelString';
		private const MOUSE_PADDING:int = 100;
		
		private var _scope:Scope;
		public function get scope():Scope
		{
			return _scope;
		}
		public function set scope(value:Scope):void
		{
			_scope = value;
		}
		
		private var _headerText:String;
		public function set headerText(value:String):void
		{
			_headerText = value;
		}
		
		private var _summaryHeaderText:String;
		public function set summaryHeaderText(value:String):void
		{
			_summaryHeaderText = value;
		}
		
		private var _summaryItemRendererClass:Class;
		public function set summaryItemRendererClass(value:Class):void
		{
			_summaryItemRendererClass = value;
		}
		
		private var _groupingItemRendererClass:Class;
		public function set groupingItemRendererClass(value:Class):void
		{
			_groupingItemRendererClass = value;
		}
		
		public function get isOpening():Boolean {
			return mx_internal::isOpening;
		}
		
		private var scrollTimer:Timer;
		private var scrollDirectionDown:Boolean;
		
		public function ADG()
		{
			super();
		}
		
		public function dispatchAdvancedDataGridEvent(type:String,item:Object, renderer:IListItemRenderer, trigger:Event = null, opening:Boolean = true, animate:Boolean = true, dispatch:Boolean = true):void {
			mx_internal::dispatchAdvancedDataGridEvent(type, item,renderer, trigger, opening, animate, dispatch);
		}
		
		private var needInvalidateRenderers:Boolean = false;
		
		public function invalidateRenderers():void
		{
			needInvalidateRenderers = true;
			invalidateProperties();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			percentHeight = 100;
			percentWidth = 50;
			variableRowHeight = true;
			
			configureColumns();
			dropEnabled = true;
			
			scrollTimer = new Timer(250);
			scrollTimer.addEventListener(TimerEvent.TIMER, scrollTimerHandler);
		}
		
		override protected function dragOverHandler(event:DragEvent):void
		{
			// super isn't called because we let renderers to accept dragDrops and not entire dataGrid
			
			if (event.localY > height - MOUSE_PADDING)
			{
				scrollDirectionDown = true;
				if (!scrollTimer.running)
				{
					scrollTimer.start();
				}
				return ;
			}
			if (event.localY < MOUSE_PADDING)
			{
				scrollDirectionDown = false;
				if (!scrollTimer.running)
				{
					scrollTimer.start();
				}
				return ;
			}
			scrollTimer.stop();
		}
		
		private function scrollTimerHandler(event:TimerEvent):void
		{
			if (scrollDirectionDown)
			{
				if (verticalScrollPosition < maxVerticalScrollPosition)
				{
					verticalScrollPosition ++ ;
				}
			}
			else
			{
				if (verticalScrollPosition > 0)
				{
					verticalScrollPosition -- ;
				}
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if ( needInvalidateRenderers )
			{
				needInvalidateRenderers = false;
				
				for each ( var a:Array in rendererArray )
				{
					for each ( var renderer:UIComponent in a )
					{
						if ( renderer is DraggableItemRenderer )
						{
							(renderer as DraggableItemRenderer).invalidateUsageFill();
							(renderer as DraggableItemRenderer).invalidateData();
						}
					}
				}
			}
		}
		
		public function refresh():void
		{
			if (dataProvider)
			{
				(dataProvider.source as GroupingCollection2).refresh(false, true);
			}
			invalidateRenderers();
		}
		
		private function configureColumns():void
		{
			sortableColumns = false;
			draggableColumns = false;
			
			var groupingColumn:AdvancedDataGridColumn = new AdvancedDataGridColumn();
			groupingColumn.width = COLUMN_WIDTH;
			groupingColumn.dataField = DISPLAY_LABEL_STRING;
			groupingColumn.headerText = _headerText;
			var summaryColumn:AdvancedDataGridColumn = new AdvancedDataGridColumn();
			summaryColumn.headerText = _summaryHeaderText;
			columns = [groupingColumn, summaryColumn];
			
			var groupingRendererProvider:AdvancedDataGridRendererProvider = new AdvancedDataGridRendererProvider();
			groupingRendererProvider.column = groupingColumn;
			groupingRendererProvider.renderer = new ClassFactory(_groupingItemRendererClass);
			var summaryRendererProvider:AdvancedDataGridRendererProvider = new AdvancedDataGridRendererProvider();
			summaryRendererProvider.column = summaryColumn;
			summaryRendererProvider.renderer = new ClassFactory(_summaryItemRendererClass);
			rendererProviders = [groupingRendererProvider, summaryRendererProvider];
			
			setStyle('defaultLeafIcon', null);
		}
	}
}